package com.example.demo.controller;

import com.example.demo.annotate.NoPackageResult;
import com.example.demo.annotate.Repeat;
import com.example.demo.common.Result;
import com.example.demo.entity.User;
import com.example.demo.entity.dto.CompanyDTO;
import com.example.demo.exception.DataException;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/repeat")
@Api(tags = "13、防重复访问管理")
@ApiSupport(author = "罗海林", order = 13)
@Validated
@Slf4j
public class RepeatController {

    @PostMapping("/test1")
    @ApiOperation(value = "访抖动测试1", response = String.class)
    @ApiOperationSupport(author = "罗海林", order = 1, includeParameters = { "name" })
    @Repeat(prefix = "luohailin", delimiter = "|", field = { "id" }, seconds = 10)
    public Result test1(@RequestBody @Validated({ CompanyDTO.Delete.class }) CompanyDTO companyDTO) {
        Result res = new Result();
        return res.ok();
    }

    @PostMapping("/test2")
    @ApiOperation(value = "访抖动测试2", response = String.class)
    @ApiOperationSupport(author = "罗海林", order = 2)
    @Repeat(prefix = "ysyl", delimiter = "@", field = { "id", "age", "name" })
    public Result test2(@RequestParam String userid,
                        @RequestParam String name,
                        @RequestParam String age) {
        Result res = new Result();
        return res.ok();
    }

    @PostMapping("/return1")
    @ApiOperation(value = "返回值封装（返回User）", response = String.class)
    @ApiOperationSupport(author = "罗海林", order = 3)
    public User return1() {
        User user = new User();
        user.setName("杨添以");
        return user;
    }

    @PostMapping("/return2")
    @ApiOperation(value = "返回值封装（返回String）", response = String.class)
    @ApiOperationSupport(author = "罗海林", order = 4)
    @NoPackageResult
    public String return2() {
        return "杨添以与林春丽";
    }

    @PostMapping("/return3")
    @ApiOperation(value = "返回值封装（返回Result）", response = String.class)
    @ApiOperationSupport(author = "罗海林", order = 5)
    public Result return3() {
        Result res = new Result();
        return res.ok();
    }

    @PostMapping("/throwError")
    @ApiOperation(value = "抛异常处理", response = String.class)
    @ApiOperationSupport(author = "罗海林", order = 6)
    public Result throwError() {
        /*String str = null;
        try {
            getData();

            String temp = "罗海林";
            //temp = str.toString();

            log.info("执行出结果：" + temp);
        }
        catch (Exception ex) {
            log.info("执行到catch了");
            throw new DataException("str是null");
        }
        finally {
            log.info("执行到finally了");
        }*/

        getList();

        log.info("执行到最后了");

        Result res = new Result();
        return res.ok();
    }

    private void getData() {
        String str = null;
        try {
            String temp = "王彦远";
            temp = str.toString();

            log.info("执行出结果2：" + temp);
        }
        catch (Exception ex) {
            log.info("执行到catch了2");
            throw new DataException("str是null2");
        }
        finally {
            log.info("执行到finally了2");
        }

        log.info("执行到最后了2");
    }

    private void getList() {
        log.info("111");
        getData();
        log.info("222");
    }
}
